package zombies;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class menuFrame extends JFrame {
    private final int WIDTH = Zombies.WIDTH;
    private final int HEIGHT = Zombies.HEIGHT;
    
    private boolean gameStarted = false;
    public JPanel mainPanel;
    
    public menuFrame() {
        this.setSize(WIDTH, HEIGHT);
        this.setLocationRelativeTo(null);
        this.setTitle("Zombies!");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        initComponents();
        
        
        mainPanel = new JPanel();
        
        mainPanel.setLayout(new GridBagLayout());
        addToPanel(mainPanel);
        
        this.add(mainPanel);
        this.setVisible(true);
    }
    
    private void startGame(JPanel panel) {
        panel.removeAll();
        panel.setLayout(new BorderLayout());
        map = new Map();
        panel.add(map, BorderLayout.CENTER);
    }
    
    private void addToPanel(JPanel panel) {
        addItem(panel, mainMenuTitle, 
                0, 0, 1, 1, GridBagConstraints.CENTER);
        addItem(panel, startGameButton,
                0, 1, 1, 1, GridBagConstraints.WEST);
        addItem(panel, continueGameButton,
                0, 2, 1, 1, GridBagConstraints.WEST);
        addItem(panel, levelSelectButton,
                0, 3, 1, 1, GridBagConstraints.WEST);
        addItem(panel, optionsButton,
                0, 4, 1, 1, GridBagConstraints.WEST);
        addItem(panel, missionEditorButton,
                0, 5, 1, 1, GridBagConstraints.WEST);
        
        addItem(panel, exitGameButton,
                2, 5, 1, 1, GridBagConstraints.EAST);
    }
    
    private void initComponents() {
        EventListener el = new EventListener();
        
        startGameButton = new JButton("Start Game");
        continueGameButton = new JButton("Continue Game");
        levelSelectButton = new JButton("Select Level");
        missionEditorButton = new JButton("Mission Editor");
        optionsButton = new JButton("Options");
        exitGameButton = new JButton("Exit Game");
        mainMenuTitle = new JLabel("MAIN MENU");
        
        startGameButton.addActionListener(el);
        continueGameButton.addActionListener(el);
        levelSelectButton.addActionListener(el);
        missionEditorButton.addActionListener(el);
        optionsButton.addActionListener(el);
        exitGameButton.addActionListener(el);
    }
    
    private void addItem(JPanel panel, JComponent component,
                                        int x, int y, int width, int height,
                                        int align) {
        GridBagConstraints menuConstraints = new GridBagConstraints();
        
        menuConstraints.gridx = x;
        menuConstraints.gridy = y;
        menuConstraints.gridwidth = width;
        menuConstraints.gridheight = height;
        menuConstraints.weightx = 266.0;
        menuConstraints.weighty = 200.0;
        menuConstraints.insets = new Insets(30, 30, 30, 30);
        menuConstraints.anchor = align;
        menuConstraints.fill = GridBagConstraints.NONE;
        panel.add(component, menuConstraints);
    }
    
    public class EventListener implements ActionListener{
        public void actionPerformed(ActionEvent event) {
            if(event.getSource() == startGameButton) {
                menuFrame mf = new menuFrame();
                mf.startGame(mf.mainPanel);
            }
            if(event.getSource() == exitGameButton) {
                System.exit(0);
            }
        }
    }
    
    private Map map;
    
    private JButton startGameButton;
    private JButton continueGameButton;
    private JButton levelSelectButton;
    private JButton missionEditorButton;
    private JButton optionsButton;
    private JButton exitGameButton;
    private JLabel mainMenuTitle;
}